//
//  GameLevelCellView.m
//  linkingGame
//
//  Created by zw wu on 15/4/11.
//  Copyright (c) 2015年 com.jinWu. All rights reserved.
//

#import "GameLevelCellView.h"

@interface GameLevelCellView ()

@property (nonatomic,retain) UILabel *levelLabel;

@end

@implementation GameLevelCellView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        float between = 10;
        if (isPhone())
        {
            between = 5;
        }
        
        UIButton *levelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        levelBtn.frame = CGRectMake(between, 0, frame.size.width - 2 * between, frame.size.height - 2 * between);
        [self addSubview:levelBtn];
        self.levelBtn = levelBtn;
        
        levelBtn.backgroundColor = [UIColor greenColor];
        
        UILabel *levelLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
        levelLabel.textAlignment = NSTextAlignmentCenter;
        levelLabel.textColor = [UIColor whiteColor];
        levelLabel.font = [UIFont boldSystemFontOfSize:40];
        levelLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:levelLabel];
        self.levelLabel = levelLabel;
        [levelLabel release];
        
        self.levelLabel.center = levelBtn.center;
        
        int starBetween = between - 2;
        for(int i = 1;i <= 3;i++)
        {
            CGRect starRect = CGRectMake(frame.size.width / 2 - 24 - starBetween + (i - 1) * (16 + starBetween), frame.size.height - 5 - starBetween, 16, 16);
            UIImageView *star = [[UIImageView alloc]initWithFrame:starRect];
            star.image = [UIImage imageNamed:@"star_none"];
            star.tag = 2000 + i;
            [self addSubview:star];
            [star release];
        }
    }
    
    return self;
}

- (void)dealloc
{
    self.levelBtn = nil;
    self.levelLabel = nil;
    [super dealloc];
}

- (void)setLevelTag:(int)levelTag
{
    _levelTag = levelTag;
    self.levelLabel.text = [NSString stringWithFormat:@"%d",levelTag];
}

- (void)resetUnlockState:(BOOL)isUnlock
{
    self.levelBtn.enabled = isUnlock;
    
    if (isUnlock)
    {
        for(int i = 1;i <= 3;i++)
        {
            UIImageView *star = (UIImageView *)[self viewWithTag:(2000 + i)];
            star.hidden = NO;
        }
    }
    else
    {
        for(int i = 1;i <= 3;i++)
        {
            UIImageView *star = (UIImageView *)[self viewWithTag:(2000 + i)];
            star.hidden = YES;
        }
    }
}

- (void)resetStars:(int)score
{
    if (score < 0 || score > 6)
    {
        return;
    }
    
    int all_count = score / 2;
    int half_count = score % 2;
    
    for(int i = 1;i <= 3;i++)
    {
        UIImageView *star = (UIImageView *)[self viewWithTag:(2000 + i)];
        if (i <= all_count)
        {
            star.image = [UIImage imageNamed:@"star_all"];
        }
        
        if (half_count > 0 && i == all_count + 1)
        {
            star.image = [UIImage imageNamed:@"star_half"];
        }
    }
}

@end
